var vm = new Vue({
	el: '#rrapp',
	data: {
		courseSmall: {
			classificationName: "分类",
			classificationId: null,
		},
		addLagName: null,
		addMidName: null,
		addSmlName: null,

		forAddMidId: null,
		forAddMid: {},
		forAddSmlFirst: {},
		forAddSmlFirstId: null,
		forAddSmlSecond: {},
		forAddSmlSecondId: null,
		forDelLag: {},
		forDelLagId: null,
		forShow: null,
		forDelMidFirst: {},
		forDelMidFirstId: null,
		forDelMidSecond: {},
		forDelMidSecondId: null,

		forDelSmlFirst: {},
		forDelSmlFirstId: null,

		courseSmallClassificationFirstId: 0,
		courseSmallClassificationFirst: {},
		courseSmallClassificationSecondId: 0,
		courseSmallClassificationSecond: {},
		courseSmallClassificationThirdId: 0,
		courseSmallClassificationThird: {},
	},
	mounted() {
		this.getCourseSmallClassificationFirst(this.courseSmallClassificationFirstId)
	},
	methods: {
		forClassificationList: function(list) {
			console.log(list)
			var accordion = document.getElementById("accordion")
			var centence = '';
			for(var i = 0; i < list.length; i++) {
				centence += '<div class="panel panel-primary">'
				centence += '<div class="panel-heading" data-toggle="collapse" data-parent="#accordion" href="#' + list[i].id + '">'
				centence += '<h4 class="panel-title">'
				centence += '<span>' + list[i].name
				centence += '</span>'
				centence += '</h4>'
				centence += '</div>'
				centence += '<div id="' + list[i].id + '" class="panel-collapse collapse">'
				centence += '<div class="panel-body">'
				if(list[i].childClassificationList != null) {
					centence += '<div class="panel-group" id="r' + list[i].id + '">'
					for(var o = 0; o < list[i].childClassificationList.length; o++) {
						centence += '<div class="panel panel-info">'
						centence += '<div class="panel-heading" data-toggle="collapse" data-parent="#r' + list[i].id + '" href="#' + list[i].childClassificationList[o].id + '">'
						centence += '<h4 class="panel-title">'
						centence += '<span>' + list[i].childClassificationList[o].name
						centence += '</span>'
						centence += '</h4>'
						centence += '</div>'
						centence += '<div id="' + list[i].childClassificationList[o].id + '" class="panel-collapse collapse">'
						centence += '<div class="panel-body">'
						if(list[i].childClassificationList[o].childClassificationList != null) {
							centence += '<div class="panel-group" id="r' + list[i].childClassificationList[o].id + '">'
							for(var p = 0; p < list[i].childClassificationList[o].childClassificationList.length; p++) {
								centence += '<div class="panel panel-default">'
								centence += 	'<div class="panel-heading">'
								centence += 		'<h4 class="panel-title">'
								centence += 			'<span>' + list[i].childClassificationList[o].childClassificationList[p].name + '</span>'
								centence += 		'</h4>'
								centence += 	'</div>'
								centence += '</div>'
							}
							centence += '</div>'
						} else {
							centence += '无下属分类！'
						}
						centence += '</div>'
						centence += '</div>'
						centence += '</div>'
					}
					centence += '</div>'
				} else {
					centence += '无下属分类！'
				}

				centence += '</div>'
				centence += '</div>'
				centence += '</div>'
			}
			accordion.innerHTML = centence
		},
		getCourseSmallClassificationFirst: function(id) {
			$.ajax({
				type: "GET",
				url: baseURL + "course/courseSmallClassification/getCourseSmallClassification?id=" + id,
				contentType: "application/json",
				success: function(r) {
					if(r.code === 0) {
						if(r.data[0].childClassificationList.size != 0) {
							vm.courseSmallClassificationFirst = r.data[0].childClassificationList
							vm.forAddMid = r.data[0].childClassificationList
							vm.forAddSmlFirst = r.data[0].childClassificationList
							vm.forDelLag = r.data[0].childClassificationList
							vm.forDelMidFirst = r.data[0].childClassificationList
							vm.forDelSmlFirst = r.data[0].childClassificationList
							vm.forShow = r.data[0].childClassificationList
							vm.forClassificationList(vm.forShow)
						}
					} else {
						alert(r.msg);
					}
				}
			});
		},
		getCourseSmallClassificationSecond: function() {
			$.ajax({
				type: "GET",
				url: baseURL + "course/courseSmallClassification/getCourseSmallClassification?id=" + vm.courseSmallClassificationFirstId,
				contentType: "application/json",
				success: function(r) {
					if(r.code === 0) {
						vm.courseSmallClassificationSecond = r.data
					} else {
						alert(r.msg);
					}
				}
			});
		},
		getForAddSmlSecond: function() {
			$.ajax({
				type: "GET",
				url: baseURL + "course/courseSmallClassification/getCourseSmallClassification?id=" + vm.forAddSmlFirstId,
				contentType: "application/json",
				success: function(r) {
					if(r.code === 0) {
						vm.forAddSmlSecond = r.data
					} else {
						alert(r.msg);
					}
				}
			});
		},
		getForDelMidSecond: function() {
			$.ajax({
				type: "GET",
				url: baseURL + "course/courseSmallClassification/getCourseSmallClassification?id=" + vm.forDelMidFirstId,
				contentType: "application/json",
				success: function(r) {
					if(r.code === 0) {
						vm.forDelMidSecond = r.data
					} else {
						alert(r.msg);
					}
				}
			});
		},
		getCourseSmallClassificationThird: function() {
			$.ajax({
				type: "GET",
				url: baseURL + "course/courseSmallClassification/getCourseSmallClassification?id=" + vm.courseSmallClassificationSecondId,
				contentType: "application/json",
				success: function(r) {
					if(r.code === 0) {
						vm.courseSmallClassificationThird = r.data
					} else {
						alert(r.msg);
					}
				}
			});
		},
		add: function(type) {
			var name
			var parentId
			if(type === "新增大分类") {
				if(vm.addLagName == null || vm.addLagName == undefined || vm.addLagName === '') {
					alert('每个内容都必须输入或选择！', function(index) {});
				} else {
					name = vm.addLagName
					parentId = -2
					vm.Addtool(parentId,name)
				}
			} else if(type === "新增中分类") {
				if(vm.forAddMidId == null || vm.forAddMidId == undefined || vm.forAddMidId === '' || vm.addMidName == null || vm.addMidName == undefined || vm.addMidName === '') {
					alert('每个内容都必须输入或选择！', function(index) {});
				} else {
					name = vm.addMidName
					parentId = vm.forAddMidId
					vm.Addtool(parentId,name)
				}
			} else if(type === "新增小分类") {
				if(vm.forAddSmlSecondId == null || vm.forAddSmlSecondId == undefined || vm.forAddSmlSecondId === '' || vm.addSmlName == null || vm.addSmlName == undefined || vm.addSmlName === '') {
					alert('每个内容都必须输入或选择！', function(index) {});
				} else {
					name = vm.addSmlName
					parentId = vm.forAddSmlSecondId
					vm.Addtool(parentId,name)
				}
			}
		},
		Addtool : function(parentId,name){
			var data = {
				"parentId": parentId,
				"name": name
			}
			$.ajax({
				type: "POST",
				url: baseURL + "course/courseSmallClassification/insertCourseSmallClassification",
				contentType: "application/json",
				data: JSON.stringify(data),
				success: function(r) {
					if(r.code === 0) {
						alert('操作成功', function(index) {
							window.location.reload()
						});
					} else {
						alert(r.msg);
					}
				}
			});
		},
		checkAndDel: function(id) {
			$.ajax({
				type: "GET",
				url: baseURL + "course/courseSmallClassification/getCourseSmallClassification?id=" + id,
				contentType: "application/json",
				success: function(r) {
					if(r.code === 0) {
						if(r.data.length != 0) {
							alert('此分类下有子分类，请先删除子分类！', function(index) {});
						} else {
							confirm('确定要删除选中的记录？', function() {
								$.ajax({
									type: "GET",
									url: baseURL + "course/courseSmallClassification/delCourseSmallClassification?id=" + id,
									contentType: "application/json",
									success: function(r) {
										if(r.code == 0) {
											alert('操作成功', function(index) {
												window.location.reload()
											});
										} else {
											alert(r.msg);
										}
									}
								});
							});
						}
					} else {
						alert(r.msg);
					}
				}
			});
		},
		del: function(type) {
			var id
			if(type === "删除大分类") {
				if(vm.forDelLagId == null || vm.forDelLagId == undefined || vm.forDelLagId === '') {
					alert('每个内容都必须输入或选择！', function(index) {});
				} else {
					id = vm.forDelLagId
				}
			} else if(type === "删除中分类") {
				if(vm.forDelMidSecondId == null || vm.forDelMidSecondId == undefined || vm.forDelMidSecondId === '') {
					alert('每个内容都必须输入或选择！', function(index) {});
				} else {
					id = vm.forDelMidSecondId
				}
			} else if(type === "删除小分类") {
				if(vm.courseSmallClassificationThirdId == null || vm.courseSmallClassificationThirdId == undefined || vm.courseSmallClassificationThirdId === '') {
					alert('每个内容都必须输入或选择！', function(index) {});
				} else {
					id = vm.courseSmallClassificationThirdId
				}
			}
			vm.checkAndDel(id)
		},
	}
});